/***************************************************************************
 *                                                                         *
 *   This file is part of the Fotowall project,                            *
 *       http://code.google.com/p/fotowall                                 *
 *                                                                         *
 *   Copyright (C) 2007-2009 by Enrico Ros <enrico.ros@gmail.com>          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 * ----------------------------------------------------------------------- *
 *																		   *
 *	Modifications for FotoKinect, Spring 2011 by Gianna Badiali,		   *
 *                                                   Gabriel Fernandez,	   *
 *                                                   Kelsey Tripp, and     *
 *                                                   Sam Swarr             *
 *																		   *
 ***************************************************************************/

#ifndef __MainWindow_h__
#define __MainWindow_h__

#include <QWidget>
#include <QLabel>
#include "ModeInfo.h"
#include "devices/InputPoint.h"

class DeviceManager;
class Desk;
class FWGraphicsView;
class QActionGroup;
class QGraphicsView;
class QMenu;
class QState;
class QStateMachine;
class QSignalTransition;
namespace Ui { class MainWindow; }

enum MenuState {  MENU_OPEN,
                  MENU_CLOSED,
                  IMPORT_MODE,
                  CANVAS_MODE,
				  LOAD_MODE,
				  EXPORT_MODE,
				  SAVE_MODE};

class MainWindow : public QWidget
{
    Q_OBJECT
    public:
        MainWindow(QWidget * parent = 0);
        ~MainWindow();

        void loadXml(const QString & filePath);
        void saveXml(const QString & filePath) const;

		void cropPhoto(QRect cropSelection);

        QString getPreviousImportFile();
        QString getNextImportFile();
		void importSelectedPhoto();

        QString getPreviousLoadFile();
        QString getNextLoadFile();
		void loadSelectedPhoto();
		void loadImages(QStringList &imagesPath);

		void makeNewDeviceManager();

    private:
        void setNormalProject();
        void addAnimationsToMenu(QSignalTransition * transition);
        void hideAllExtraWidgets(QState * state);
		void updateLoadFiles();
		void updateImportFiles();
		void initForLoading();
		void initForImporting();

        Ui::MainWindow * ui;
        Desk *          m_desk;
        DeviceManager * m_deviceManager;
        ModeInfo        m_modeInfo;
        Qt::WindowFlags m_windowFlags;
        QActionGroup *  m_gBackActions;
        QStateMachine * m_machine;

		bool m_firstTimeInEmptyState;
        bool    m_firstCursor;
        bool	m_showCursorOneFoundInstr;
        bool	m_showCursorTwoFoundInstr;
        bool	m_menuOpen;
        bool	m_importMenuOpen;
        bool	m_waiting;
        bool	m_lastOnMenu;

        int m_importIndex;
		int m_loadIndex;

        QLabel m_importMenu;
        QStringList m_importFiles;
		QStringList m_loadFiles;

        QState * m_importState;
		QState * m_chooseCropPhotoState;
        QState * m_cropState;
		QState * m_colorState;
		QState * m_menuState;
		QState * m_emptyState;
		QState * m_helpState;
		QState * m_loadState;

		//timers for the tips that pop up when cursor is lost/found
		QTimer *m_hideLostInstr1Timer;
		QTimer *m_hideLostInstr2Timer;
		QTimer *m_hideLostInstrBeginTimer;
		QTimer *m_cursorDetectedTimer;
		QTimer *m_cursorDetected2Timer;
		//timers for notifications related to export/erase/save/crop.
		QTimer *m_savedTimer;
		QTimer *m_exportedTimer;
		QTimer *m_erasedTimer;
		QTimer *m_noPicturesToCropTimer;
		//timers for switching cursors back to normal after a select gesture
		QTimer *m_cursor1GreenTimer;
		QTimer *m_cursor2GreenTimer;
		QTimer *m_connectedNotificationTimer;

		QString parseProjectDate(QString);
		QString parseProjectTime(QString);

		QPoint m_cropP1;
		QPoint m_cropP2;

    public Q_SLOTS:
        void slotAddInput(InputPoint p);
        void slotRemoveInput(int id, bool switchedCursors);
        void slotUpdateCursor(InputPoint p);
        void slotSelect(InputPoint id);
        void slotDeselect(InputPoint id);
		void slotDeselectAll();
        void slotBrowseImportMenu(int direction); /* LEFT == -1, RIGHT == 1 */
		void slotBrowseLoadMenu(int direction);
		void slotRevertCursor1();
		void slotRevertCursor2();

		//methods for setting cursor appearance
		void setCursorGreen(int cursorID);
		void setCursorRed(int cursorID);
		
		//slota for knowing when the kinect has connected
		void slotDeviceConnected();
		void slotDeviceDisconnected();

    private Q_SLOTS:
		//slots for buttons.
        void on_saveButton_clicked();
        void on_exportButton_clicked();
        void on_quitButton_clicked();

		//slots for hiding notifications
        void slotHideLostCursorInstr();
		void slotHideLostCursorInstr2();
		void slotHideLostCursorInstrBegin();
        void slotHideCursorDetected();
		void slotHideCursorDetected2();
		void slotHideExportNotification();
		void slotHideSaveNotification();
		void slotHideEraseNotification();
		void slotHideNoPicturesToCrop();
		void slotHideConnectedNotification();


		signals:
			void pictureImported();
			void projectSaved();
			void projectExported();
			void projectLoaded();
			void cropPhotoSelected();
            void pictureCropped();
			void colorChanged();
};

#endif
